package io.renren.modules.sp.controller.front;

import io.renren.common.annotation.LogOperation;
import io.renren.common.interceptor.UserContext;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.Result;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.sp.dto.SpMatchItemTeamDTO;
import io.renren.modules.sp.dto.SpTeamUserDTO;
import io.renren.modules.sp.dto.SpUserDTO;
import io.renren.modules.sp.entity.SpMatchItemTeamEntity;
import io.renren.modules.sp.entity.SpMatchItemTeamUserEntity;
import io.renren.modules.sp.service.SpMatchItemService;
import io.renren.modules.sp.service.SpMatchItemTeamService;
import io.renren.modules.sp.service.SpMatchItemTeamUserService;
import io.renren.modules.sp.service.SpTeamUserService;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
*
*
* @author Mark sunlightcs@gmail.com
* @since 3.0 2023-04-20
*/
@RestController
@RequestMapping("front/spmatchItemTeam")
@Api(tags="领队成员")
public class FrontMatchItemTeamController {

    @Autowired
    private SpTeamUserService spTeamUserService;
    @Autowired
    private SpMatchItemService spMatchItemService;
    @Autowired
    private SpMatchItemTeamService spMatchItemTeamService;
    @Autowired
    private SpMatchItemTeamUserService spMatchItemTeamUserService;


    /**
     * 报名赛项
     * @return
     */
    @PostMapping("save")
    @LogOperation("报名赛项")
    public Result save(@RequestBody SpMatchItemTeamDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        //小队名称不能重复
        Map<String,Object> inParams = new HashMap<>();
        inParams.put("teamName",dto.getTeamName());
        inParams.put("matchItemId",dto.getMatchItemId());
        dto.setSignTime(new Date());
        return spMatchItemTeamService.saveMatchItemTeam(dto);
    }

    /**
     * 报名赛项
     * @return
     */
    @PostMapping("savePerson")
    @LogOperation("报名赛项")
    public Result savePerson(@RequestBody SpMatchItemTeamDTO dto){
        //SpUserDTO user =UserContext.getUser();
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        //小队名称不能重复
        Map<String,Object> inParams = new HashMap<>();
        inParams.put("teamName",dto.getTeamName());
        inParams.put("matchItemId",dto.getMatchItemId());
        dto.setSignTime(new Date());
        return spMatchItemTeamService.saveMatchItemPerson(dto);
    }

    /**
     * 报名赛项
     * @return
     */
    @PostMapping("cancel")
    @LogOperation("取消报名")
    public Result cancel(@RequestBody  SpMatchItemTeamDTO dto ){
        SpUserDTO user =UserContext.getUser();
        SpMatchItemTeamEntity entity = spMatchItemTeamService.selectById(dto.getId());
        if(!entity.getTeamId().equals(user.getTeamId())){
            return new Result().error("暂无权限！");
        }
        return spMatchItemTeamService.cancel(dto);
    }


    /**
     * 抽签排序
     * @param id
     * @return
     */
    @GetMapping("draw")
    @LogOperation("抽签排序")
    public Result draw(@RequestParam Long id){
        return spMatchItemTeamService.draw(id);
    }


    /**
     * 申请逻辑
     * @return
     */
    @GetMapping("rise")
    @LogOperation("申请逻辑")
    public Result rise(@RequestParam Long id,String riseFile,String promissFile,String users,String groupName, String teamId) {
        //效验数据
        return spMatchItemTeamService.confirmRise(id, riseFile, promissFile, users, groupName, teamId);
    }

    /**
     * 修改信息
     * @return
     */
    @GetMapping("updTeamBaseInfo")
    @LogOperation("修改信息")
    public Result updTeamBaseInfo(@RequestParam Long id,String users,String groupName,String teachers) {
        //效验数据
        return spMatchItemTeamService.updTeamBaseInfo(id, users, groupName, teachers);
    }

    /**
     * 后台通过Excel导入的，需要领队确认晋级
     * @return
     */
    @GetMapping("rise2")
    @LogOperation("确认晋级")
    public Result rise2(@RequestParam Long id,String riseFile,String promissFile,String users,String groupName){

        // 报名表和承诺书不能同时为空
        if("null".equals(riseFile) && "null".equals(promissFile)){
            return new Result().error("报名表和承诺书至少上传一项!");
        }
        //效验数据
        return spMatchItemTeamService.confirmRise2(id,riseFile,promissFile,users,groupName);
    }

    /**
     * 重新上传晋级文件
     * @param
     * @return
     */
    @GetMapping("reUploadRiseFile")
    @LogOperation("重新上传晋级文件")
    public Result reUploadRiseFile(@RequestParam Long id,String riseFile, String promissFile, String users,String groupName, String teachers){
        //效验数据
        return spMatchItemTeamService.reUploadRiseFile(id,riseFile,promissFile,users,groupName,teachers);
    }


    @PostMapping("update")
    @LogOperation("修改")
    public Result update(@RequestBody SpTeamUserDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        spTeamUserService.update(dto);
        return new Result();
    }

    @GetMapping("queryScore")
    public Result<Map<String,Object>> queryScore(@RequestParam Long id){
        Map<String,Object> result = spMatchItemTeamService.queryScoreResult(id);
        return new Result<Map<String,Object>>().ok(result);
    }

    // 修改代表队老师
    @PostMapping("updateTeacher")
    @LogOperation("修改代表队老师")
    public Result updateTeacher(@RequestBody SpMatchItemTeamDTO dto){
        boolean result = spMatchItemTeamService.updateTeacher(dto);
        return new Result().ok(result);
    }

    // 查询是否有晋级
    @GetMapping("queryRise")
    public Result<List<SpMatchItemTeamEntity>> queryRise(@RequestParam Long teamId){
        List<SpMatchItemTeamEntity> list = spMatchItemTeamService.queryRise(teamId);
        return new Result().ok(list);
    }

    /**
     * 判断是否可以申请晋级
     * @param teamId
     * @return
     */
    /*@GetMapping("canRise")
    public Result<Boolean> canRise(@RequestParam Long teamId){
        Date date = DateUtils.parse("2023-04-20 00:00:00", DateUtils.DATE_TIME_PATTERN);
        Date sysDate = new Date();
        //系统时间超过定义时间可以申请晋级
        if(sysDate.after(date)){
            return new Result<Boolean>().ok(true);
        }else{
            return new Result<Boolean>().ok(false);
        }
    }*/



}
